package com.zmm.merchant.service.impl;

import com.zmm.common.base.model.ResultCode;
import com.zmm.common.enums.RecordType;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.merchant.dao.MerchantDao;
import com.zmm.merchant.model.Merchant;
import com.zmm.merchant.service.MerchantService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 900045
 * @description:
 * @name MerchantServiceImpl
 * @date By 2021-07-06 11:22:51
 */
@Service
public class MerchantServiceImpl implements MerchantService {
	
	
	@Resource
	private MerchantDao merchantDao;

	/**
	 * 新增商家业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-06 11:33:27
	 * @throws 
	 * @param merchant: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addMerchant(Merchant merchant) throws BusinessException {
		merchant.setCreateTime(System.currentTimeMillis());
		Integer insert = merchantDao.insertMerchant(merchant);
		if (insert > 0){
			return true;
		}
		throw new BusinessException(ResultCode.INSERT_FAILED, RecordType.SAVE_MERCHANT,"addMerchant");
	}

	/**
	 * 根据 商家ID 查询商家信息业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-06 15:09:53
	 * @throws 
	 * @param merchantId: 
	 * @return: com.zmm.merchant.model.Merchant
	 **/
	@Override
	public Merchant findOneMerchantByMerchantId(Integer merchantId) {
		return merchantDao.selectOneMerchantByMerchantId(merchantId);
	}

	/**
	 * 更新商家信息业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-06 15:25:00
	 * @throws 
	 * @param merchant: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyMerchant(Merchant merchant) {
		merchant.setUpdateTime(System.currentTimeMillis());
		Integer update = merchantDao.updateMerchant(merchant);
		if (update > 0){
			return true;
		}
		return false;
	}


	/**
	 * 根据 商家ID 更新状态业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-06 15:50:00
	 * @throws 
	 * @param updateStatus: 要更新的状态
	 * @param merchantId: 商家ID
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyMerchantStatus(Integer updateStatus,Integer merchantId) {
		Merchant merchant = this.findOneMerchantByMerchantId(merchantId);
		
		Integer update = merchantDao.updateMerchantStatus(updateStatus,System.currentTimeMillis(),merchantId,merchant.getStatus());
		if (update > 0){
			return true;
		}
		return false;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyMerchantSoldOut(Integer soldOut, Integer merchantId) throws BusinessException {
		Integer update = merchantDao.updateMerchantSoldOut(soldOut,System.currentTimeMillis(),merchantId);
		if (update > 0){
			return true;
		}
		throw new BusinessException(ResultCode.UPDATE_FAILED, RecordType.MERCHANT_SOLD_OUT,"modifyMerchantSoldOut");
	}
}
